package com.steven.sync.mianshi;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;

/**
 * 一个容器两个方法 add ,size,
 * 两个线程 一个线程add 10次 另外一个线程监听到大小等于5 的时候给出提示并结束
 */
public class Test1_LockSupport {

    //    volatile修饰引用类型改变了其他线程是看不到的，除非引用本身改了
    static volatile List list = Collections.synchronizedList(new ArrayList<>());


    public void add(Object o) {
        list.add(o);
    }

    public Integer size() {
        return list.size();
    }

    static  Thread thread1=null,thread2=null;

    public static void main(String[] args) {
        Test1_LockSupport test1  =new Test1_LockSupport();

         thread1 = new Thread(() -> {
             LockSupport.park();
            System.out.println("size已经等于5");
             LockSupport.unpark(thread2);
            //继续运行第一个线程
        });

         thread2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                test1.add(new Object());
                if (test1.size() == 5) {
                 LockSupport.unpark(thread1);
                  LockSupport.park();
                }
            }
        });

         thread1.start();
         thread2.start();

    }


}
